package com.nathan.crm.controller;

import com.nathan.base.BaseController;
import com.nathan.base.ResultInfo;
import com.nathan.crm.query.CustomerServeQuery;
import com.nathan.crm.service.CustomerServeService;
import com.nathan.crm.utils.LoginUserUtil;
import com.nathan.crm.vo.CustomerServe;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

@Controller
@RequestMapping("customer_serve")
public class CustomerServeController extends BaseController {
    @Autowired
    private CustomerServeService customerServeService;

    /**
     * 根据type跳转到不同页面
     * @param type
     * @return
     */
    @RequestMapping("index/{type}")
    public String index(@PathVariable("type") Integer type){
        switch (type){
            case 1:
                return "customerServe/customer_serve";
            case 2:
                return "customerServe/customer_serve_assign";
            case 3:
                return "customerServe/customer_serve_proce";
            case 4:
                return "customerServe/customer_serve_feed_back";
            case 5:
                return "customerServe/customer_serve_archive";
            default:
                return "";
        }
    }

    @RequestMapping("list")
    @ResponseBody
    public Map<String,Object> list(CustomerServeQuery customerServeQuery, Integer flag, HttpServletRequest request){
        // 判断是否是服务处理
        if(flag != null && flag == 1){
            // 从cookie中获取当前登录用户id
            int assigner = LoginUserUtil.releaseUserIdFromCookie(request);
            customerServeQuery.setAssigner(assigner);
        }
        return customerServeService.queryByParamsForTable(customerServeQuery);
    }

    /**
     * 打开创建服务页面
     * @return
     */
    @RequestMapping("addCustomerServePage")
    public String addCustomerServePage(){
        return "customerServe/customer_serve_add";
    }

    /**
     * 添加创建服务
     * @param customerServe
     * @return
     */
    @RequestMapping("add")
    @ResponseBody
    public ResultInfo addCreateServe(CustomerServe customerServe){
        customerServeService.addCreateServe(customerServe);
        return success("添加成功！");
    }

    /**
     * 打开分配服务页面
     * @param id
     * @return
     */
    @RequestMapping("openCustomerServeAssignPage")
    public String addCustomerServeAssignPage(Integer id, Model model){
        CustomerServe customerServe = customerServeService.selectByPrimaryKey(id);
        model.addAttribute("customerServe",customerServe);
        return "customerServe/customer_serve_assign_add";
    }

    /**
     * 更新客户服务
     * @param customerServe
     * @return
     */
    @RequestMapping("update")
    @ResponseBody
    public ResultInfo updateCustomerServe(CustomerServe customerServe){
        customerServeService.updateCustomerServe(customerServe);
        return success("更新成功！");
    }

    /**
     * 打开服务处理页面
     * @param id 服务id
     * @return
     */
    @RequestMapping("openCustomerServeProcePage")
    public String openCustomerServeProcePage(Integer id,Model model){
        CustomerServe customerServe = customerServeService.selectByPrimaryKey(id);
        model.addAttribute("customerServe",customerServe);
        return "customerServe/customer_serve_proce_add";
    }

    /**
     * 打开服务反馈页面
     * @param id
     * @param model
     * @return
     */
    @RequestMapping("openCustomerServeBackPage")
    public String openCustomerServeBackPage(Integer id,Model model){
        CustomerServe customerServe = customerServeService.selectByPrimaryKey(id);
        model.addAttribute("customerServe",customerServe);

        return "customerServe/customer_serve_feed_back_add";
    }
}
